package com.yxjx.duoxue;

import java.util.ArrayList;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.StrictMode;

import com.yxjx.duoxue.datamodel.DMBaiduLocation;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.utils.Utils;

public class YxApplication extends Application {
	
	private ArrayList<Activity> mList = new ArrayList<Activity>();
	
	// 百度定位
	public LocationClient mLocationClient = null;
	public BDLocationListener myListener = new MyLocationListener();
	
	@SuppressLint("NewApi") @Override
	public void onCreate() {
		super.onCreate();
		
		initLocationClient();
	    
		AppErrorHandler errorHandler = AppErrorHandler.getInstance();
		errorHandler.init(getApplicationContext(), this);
		
		// 初始化strictmode
		if (Development.DEV_MODE) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
					.detectDiskReads().detectDiskWrites().detectNetwork()
					.penaltyLog().build());
		} 
	}

	private void initLocationClient() {
		// 百度地图初始化
		SDKInitializer.initialize(getApplicationContext());
		// 百度定位
		mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
		if (Globals.getInstance(this).getLocationClient() == null) {
			Globals.getInstance(this).setLocationClient(mLocationClient);
		}
		
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
		option.setOpenGps(true);
		option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(10 * 60 * 1000);//设置发起定位请求的间隔时间为10min
		option.setIsNeedAddress(true);//返回的定位结果包含地址信息
		option.setNeedDeviceDirect(false);//返回的定位结果包含手机机头的方向
		mLocationClient.setLocOption(option);
	    mLocationClient.registerLocationListener( myListener );    //注册监听函数
	    mLocationClient.start();
	    mLocationClient.requestLocation();
	}
	
	/**
	 * 用于列表页面重新请求当前位置,
	 * 用于在course list中使用
	 */
	public void requestRelocation() {
		// 尝试打开地图
        LocationClient client = Globals.getInstance(this).getLocationClient();
        if (client != null) {
        	if (!client.isStarted()) {
        		client.start();
        		client.getLocOption().setOpenGps(true);
        	}
        	client.requestLocation();
        	Utils.logd("location:client.requestLocation() end");
        } else {
        	initLocationClient();
        	Utils.logd("location:initLocationClient end");
        }
	}
	
	/**
	 * @deprecated
	 * @param item
	 */
	public void addActivity(Activity item) {
		mList.add(item);
	}
	
	/**
	 * @deprecated
	 * @return
	 */
	public ArrayList<Activity> getAllActivites() {
		return mList;
	}
	
	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}
			DMBaiduLocation myLocation = new DMBaiduLocation();
			myLocation.setTime(location.getTime());
			myLocation.setLocType(location.getLocType());
			myLocation.setLatitude(location.getLatitude());
			myLocation.setLongtitude(location.getLongitude());
			myLocation.setRadius(location.getRadius());
			myLocation.setCity(location.getCity());
			myLocation.setDistrict(location.getDistrict());
			myLocation.setAddrStr(location.getAddrStr());
			
			Utils.logd("onReceiveLocation BDLocation = " + location);
			Utils.logd("onReceiveLocation DMBaiduLocation = " + myLocation);
			myLocation.saveToSharedPreference(YxApplication.this);
			
			// 尝试关闭GPS
			Utils.tryCloseGps(YxApplication.this);
		}
	}
	
}
